﻿using Asmodat.Abbreviate;
using Asmodat.Extensions.Collections.Generic;
using Asmodat.Extensions.Objects;
using Asmodat.IO;
using Asmodat.Types;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Asmodat_ELM
{
    public partial class Form1 : Form
    {
        private void TTbxSearchMask_TextChanged(object sender, EventArgs e)
        {
            this.SaveState_FileManager();
        }


        private List<string> GetFileScan()
        {
            List<string> result = new List<string>();
            string searchMask = TTbxSearchMask.Text;

            if (searchMask.IsNullOrEmpty())
                searchMask = "*.*";

            var split = searchMask.SplitSafe(',');


            if (!split.IsNullOrEmpty())
            {
                foreach(string mask in split)
                {
                    if (mask.IsNullOrEmpty())
                        continue;

                    result.AddRangeDistinct(Directory.GetFiles(Directories.Current, mask));
                }
            }
            else
                result.AddRangeDistinct(Directory.GetFiles(Directories.Current, searchMask));

            result.Sort();
            return result;//.SortAscending(x => Files.GetNameWithoutExtention(x)).ToList();
        }


        private List<string> GetAllFileNames()
        {
            var scan = GetFileScan();
            List<string> result = new List<string>();

            if (!scan.IsNullOrEmpty())
                foreach (var f in scan)
                    result.Add(Files.GetName(f));

            return result;
        }

        private ThreadedDictionary<string, TickTime> UploadedFiles = new ThreadedDictionary<string, TickTime>();
        private TickTime TimeUpload = TickTime.MinValue;
        private List<string> GetChangedFileNames()
        {
            var scan = GetFileScan(); 
            List<string> result = new List<string>();

            if (!scan.IsNullOrEmpty()) 
                foreach (var f in scan)
                {
                    FileInfo info = new FileInfo(f);
                    string name = Files.GetName(f);
                    TickTime TimeUpload;
                    if (UploadedFiles.ContainsKey(name))
                        TimeUpload = UploadedFiles[name];
                    else
                    {
                        TimeUpload = TickTime.MinValue;
                        UploadedFiles.Add(name, TimeUpload);
                    }

                    
                    if (info.LastWriteTimeUtc.ToTickTime() > TimeUpload ||
                        info.CreationTimeUtc.ToTickTime() > TimeUpload)
                        result.Add(Files.GetName(name));
                }


            return result;
        }

    }
}
